﻿using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine;
using PloobsEngine.Cameras;
using PloobsEngine.Input;
using Microsoft.Xna.Framework;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Features;
using PloobsEngine.Commands;
using PloobsEngineDemo3;
using PloobsEngine.Light;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Draw;

namespace Etapa1.Screens
{    
    /// <summary>
    /// Paralax Effect and Normal Mapping Screen
    /// </summary>
    public class ParalaxScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;

        public ParalaxScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);                                    
        }
        CustomDeferred paralax;
        
        public override void LoadContent(EngineStuff es)
        {
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            base.LoadContent(es);

            #region Models

            ///Model with Paralax Shader enabled
            {
                ///Need to load the height, the normal texture and the difuse texture
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cubo", "..\\Content\\Textures\\color_map");
                sm.SetTexture("Textures\\normal_map", TextureType.BUMP);
                sm.SetTexture("Textures\\height_map", TextureType.PARALAX);
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(200, 110, 0), 1, 5, new Vector3(100, 100, 100));
                ///Enable paralax and Normal;
                paralax = new CustomDeferred(false, true, false, true);
                paralax.SpecularIntensity = 0.2f;
                paralax.SpecularPower = 30;
                paralax.Initialize();
                IMaterial mat = new Material(paralax);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }


            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero, sm);
                CustomDeferred shader = new CustomDeferred(false, false, false, false);
                shader.SpecularIntensity = 0;
                shader.SpecularPower = 0;
                shader.Initialize();
                IMaterial mat = new Material(shader);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }


            #endregion

            cam = new CameraFirstPerson(true);
            cam.FarPlane = 3000;

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);

            #region NormalLight
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion

            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);

            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            mundo.AddCamera(cam);
            this.World = mundo;

            {
                SimpleConcreteKeyboardInputPlayable ik = new SimpleConcreteKeyboardInputPlayable(StateKey.DOWN, Keys.H, aumentaScale, EntityType.TOOLS);
                BindKeyCommand bk = new BindKeyCommand(ik, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bk);
            }
            {
                SimpleConcreteKeyboardInputPlayable ik = new SimpleConcreteKeyboardInputPlayable(StateKey.DOWN , Keys.G, diminuiScale, EntityType.TOOLS);
                BindKeyCommand bk = new BindKeyCommand(ik, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bk);
            }
            {
                SimpleConcreteKeyboardInputPlayable ik = new SimpleConcreteKeyboardInputPlayable(StateKey.DOWN, Keys.Y, aumentaBias, EntityType.TOOLS);
                BindKeyCommand bk = new BindKeyCommand(ik, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bk);
            }
            {
                SimpleConcreteKeyboardInputPlayable ik = new SimpleConcreteKeyboardInputPlayable(StateKey.DOWN, Keys.T, diminuiBias, EntityType.TOOLS);
                BindKeyCommand bk = new BindKeyCommand(ik, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bk);
            }

            {
                SimpleConcreteKeyboardInputPlayable ik = new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, Keys.J, EnableDisableParalax, EntityType.TOOLS);
                BindKeyCommand bk = new BindKeyCommand(ik, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bk);
            }
            {
                SimpleConcreteKeyboardInputPlayable ik = new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, Keys.K, EnableDisableBump, EntityType.TOOLS);
                BindKeyCommand bk = new BindKeyCommand(ik, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bk);
            }
        }

        public void aumentaScale(InputPlayableKeyBoard ipk)
        {
           paralax.ScaleBias = new Vector2(paralax.ScaleBias.X + 0.001f, paralax.ScaleBias.Y);         
        }
        public void diminuiScale(InputPlayableKeyBoard ipk)
        {
            paralax.ScaleBias = new Vector2(paralax.ScaleBias.X - 0.001f, paralax.ScaleBias.Y);                    
        }
        public void aumentaBias(InputPlayableKeyBoard ipk)
        {
            paralax.ScaleBias = new Vector2(paralax.ScaleBias.X, paralax.ScaleBias.Y + 0.001f);                    
        }
        public void diminuiBias(InputPlayableKeyBoard ipk)
        {
            paralax.ScaleBias = new Vector2(paralax.ScaleBias.X, paralax.ScaleBias.Y - 0.001f);                    
        }
        public void EnableDisableParalax(InputPlayableKeyBoard ipk)
        {
            paralax.UseParalax = !paralax.UseParalax;
        }
        public void EnableDisableBump(InputPlayableKeyBoard ipk)
        {
            paralax.UseBump = !paralax.UseBump;
        }

        public override void Draw(GameTime gameTime)
        {
            Drawing.Draw2dTextAt2dLocation("Paralax " + paralax.UseParalax, new Vector2(20, 20), Color.White);
            Drawing.Draw2dTextAt2dLocation("Paralax ScaleBias " + paralax.ScaleBias, new Vector2(20, 40), Color.White);                        
            Drawing.Draw2dTextAt2dLocation("Bump " + paralax.UseBump, new Vector2(20, 60), Color.White);
            base.Draw(gameTime);
        }
    }
}

